package com.lwh.springboot.controller;

import com.lwh.springboot.pojo.entity.InsureType;
import com.lwh.springboot.util.PageInfo;
import com.lwh.springboot.service.IInsureTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/insureType")
public class InsureTypeController {
    @Autowired
    private IInsureTypeService insureTypeService;

    @RequestMapping("/toUpdate")
    public String toUpdate(Integer id, Model model) {
        InsureType insureType = insureTypeService.selectById(id);
        model.addAttribute("insureType", insureType);
        return "/insure_type/update";
    }

    @RequestMapping("/update")
    public String update(InsureType insureType) {
        insureTypeService.update(insureType);
        return "redirect:/insureType/selectByPage";
    }

    @RequestMapping("/toAdd")
    public String toAdd() {
        return "/insure_type/add";
    }

    @RequestMapping("/add")
    public String add(InsureType insureType) {
        insureTypeService.add(insureType);
        return "redirect:/insureType/selectByPage";
    }


    // /insureType/deleteById?id=23
    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        insureTypeService.deleteById(id);
        //redirect:重定向，告诉浏览器发送请求 /insureType/selectAll
        //删除完之后，应该查找展示最新的数据
        return "redirect:/insureType/selectByPage";
        //return "/insureType/selectAll";
    }

    @RequestMapping("/deleteAll")
    public String deleteAll(Integer[] ids) {
        insureTypeService.deleteAll(ids);
        return "redirect:/insureType/selectByPage";
    }

    @RequestMapping("/selectByPage")
    public String selectByPage(@RequestParam(defaultValue = "1") Integer pageNo,
                               @RequestParam(defaultValue = "10") Integer pageSize, Model model) {
        System.out.println("InsureTypeController.selectByPage");
        PageInfo pageInfo = insureTypeService.selectByPage(pageNo, pageSize);
        System.out.println("pageInfo:" + pageInfo);
        //把pageInfo数据放到内存里面
        model.addAttribute("pageInfo", pageInfo);
        //转发到insureType_list界面展示
        return "/insure_type/list";
    }

    @RequestMapping("/selectAll")
    public String selectAll(Model model) {
        // soutm
        System.out.println("InsureTypeController.selectAll");
        List<InsureType> list = insureTypeService.selectAll();

        //把list数据放到内存里面
        model.addAttribute("list", list);
        //转发到insureType_list界面展示
        return "/insure_type/list";
    }


    @RequestMapping("/selectAll1")
    @ResponseBody
    public List<InsureType> selectAll1() {
        // soutm
        System.out.println("InsureTypeController.selectAll");
        List<InsureType> list = insureTypeService.selectAll();

        return list;
    }
}